package org.mine.design.builder.core;

import org.mine.design.builder.controller.param.RequestParam;
import org.mine.design.builder.enums.DriverTypeEnum;
import org.mine.design.builder.enums.EarningsTypeEnum;
import org.mine.design.builder.service.ActivityAwardCalculateService;
import org.mine.design.builder.service.CostCalculateService;
import org.mine.design.builder.service.SubsidyCalculateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

@Service
public class OfficialEarningsBuilder extends EarningsBuilder {


    @Autowired
    private List<CostCalculateService> costCalculateServices;

    @Autowired
    private ActivityAwardCalculateService activityAwardCalculateService;

    @Autowired
    private SubsidyCalculateService subsidyCalculateService;

    @Override
    public DriverTypeEnum getDriverType() {
        return DriverTypeEnum.OFFICIAL;
    }


    @Override
    public List<EarningsResult> buildIncome(RequestParam param) {
        return Arrays.asList(
                //用户支付费用
                new EarningsResult(
                        EarningsTypeEnum.USER_PAYMENT,
                        costCalculateServices.stream()
                                .map(service -> service.calculate(param))
                                .reduce(Integer::sum)
                                .orElse(0)),
                //平台活动奖励
                new EarningsResult(EarningsTypeEnum.ACTIVITY_AWARD, activityAwardCalculateService.calculate(param)),
                //平台补贴
                new EarningsResult(EarningsTypeEnum.SUBSIDY, subsidyCalculateService.calculate(param))
        );
    }

    @Override
    public List<EarningsResult> buildDeduction(RequestParam param) {
        if(param.getViolation()){
            return Collections.singletonList(
                    new EarningsResult(
                            EarningsTypeEnum.DEDUCTION,
                            5)
            );
        }
        return Collections.emptyList();
    }
}
